<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      go/2-Go语言基础/27-组合取代继承 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>go/2-Go语言基础/27-组合取代继承</h2>



  <p class="post-date">2019-02-10</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><hr>
<p>title: “27-组合取代继承”<br>date: 2019-03-10 08:08<br>tags:</p>
<ul>
<li>Go</li>
</ul>
<hr>
<p>Go 不支持继承，但它支持组合（Composition）。组合一般定义为“合并在一起”。汽车就是一个关于组合的例子：一辆汽车由车轮、引擎和其他各种部件组合在一起。</p>
<h2 id="通过嵌套结构体进行组合"><a href="#通过嵌套结构体进行组合" class="headerlink" title="通过嵌套结构体进行组合"></a>通过嵌套结构体进行组合</h2><p>在 Go 中，通过在结构体内嵌套结构体，可以实现组合。</p>
<p>组合的典型例子就是博客帖子。每一个博客的帖子都有标题、内容和作者信息。使用组合可以很好地表示它们。通过学习本教程后面的内容，我们会知道如何实现组合。</p>
<p>我们首先创建一个 <code>author</code> 结构体。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> author <span class="keyword">struct</span> &#123;  </span><br><span class="line">    firstName <span class="keyword">string</span></span><br><span class="line">    lastName  <span class="keyword">string</span></span><br><span class="line">    bio       <span class="keyword">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(a author)</span> <span class="title">fullName</span><span class="params">()</span> <span class="title">string</span></span> &#123;  </span><br><span class="line">    <span class="keyword">return</span> fmt.Sprintf(<span class="string">"%s %s"</span>, a.firstName, a.lastName)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的代码片段中，我们创建了一个 <code>author</code> 结构体，<code>author</code> 的字段有 <code>firstname</code>、<code>lastname</code> 和 <code>bio</code>。我们还添加了一个 <code>fullName()</code> 方法，其中 <code>author</code> 作为接收者类型，该方法返回了作者的全名。</p>
<p>下一步我们创建 <code>post</code> 结构体。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> post <span class="keyword">struct</span> &#123;  </span><br><span class="line">    title     <span class="keyword">string</span></span><br><span class="line">    content   <span class="keyword">string</span></span><br><span class="line">    author</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(p post)</span> <span class="title">details</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Title: "</span>, p.title)</span><br><span class="line">    fmt.Println(<span class="string">"Content: "</span>, p.content)</span><br><span class="line">    fmt.Println(<span class="string">"Author: "</span>, p.author.fullName())</span><br><span class="line">    fmt.Println(<span class="string">"Bio: "</span>, p.author.bio)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>post</code> 结构体的字段有 <code>title</code> 和 <code>content</code>。它还有一个嵌套的匿名字段 <code>author</code>。该字段指定 <code>author</code> 组成了 <code>post</code> 结构体。现在 <code>post</code> 可以访问 <code>author</code> 结构体的所有字段和方法。我们同样给 <code>post</code> 结构体添加了 <code>details()</code> 方法，用于打印标题、内容和作者的全名与简介。</p>
<p>一旦结构体内嵌套了一个结构体字段，Go 可以使我们访问其嵌套的字段，好像这些字段属于外部结构体一样。所以上面第 11 行的 <code>p.author.fullName()</code> 可以替换为 <code>p.fullName()</code>。于是，<code>details()</code> 方法可以重写，如下所示：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(p post)</span> <span class="title">details</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Title: "</span>, p.title)</span><br><span class="line">    fmt.Println(<span class="string">"Content: "</span>, p.content)</span><br><span class="line">    fmt.Println(<span class="string">"Author: "</span>, p.fullName())</span><br><span class="line">    fmt.Println(<span class="string">"Bio: "</span>, p.bio)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在，我们的 <code>author</code> 和 <code>post</code> 结构体都已准备就绪，我们来创建一个博客帖子来完成这个程序。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> author <span class="keyword">struct</span> &#123;  </span><br><span class="line">    firstName <span class="keyword">string</span></span><br><span class="line">    lastName  <span class="keyword">string</span></span><br><span class="line">    bio       <span class="keyword">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(a author)</span> <span class="title">fullName</span><span class="params">()</span> <span class="title">string</span></span> &#123;  </span><br><span class="line">    <span class="keyword">return</span> fmt.Sprintf(<span class="string">"%s %s"</span>, a.firstName, a.lastName)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> post <span class="keyword">struct</span> &#123;  </span><br><span class="line">    title   <span class="keyword">string</span></span><br><span class="line">    content <span class="keyword">string</span></span><br><span class="line">    author</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(p post)</span> <span class="title">details</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Title: "</span>, p.title)</span><br><span class="line">    fmt.Println(<span class="string">"Content: "</span>, p.content)</span><br><span class="line">    fmt.Println(<span class="string">"Author: "</span>, p.fullName())</span><br><span class="line">    fmt.Println(<span class="string">"Bio: "</span>, p.bio)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    author1 := author&#123;</span><br><span class="line">        <span class="string">"Naveen"</span>,</span><br><span class="line">        <span class="string">"Ramanathan"</span>,</span><br><span class="line">        <span class="string">"Golang Enthusiast"</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    post1 := post&#123;</span><br><span class="line">        <span class="string">"Inheritance in Go"</span>,</span><br><span class="line">        <span class="string">"Go supports composition instead of inheritance"</span>,</span><br><span class="line">        author1,</span><br><span class="line">    &#125;</span><br><span class="line">    post1.details()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面程序中，main 函数在第 31 行新建了一个 <code>author</code> 结构体变量。而在第 36 行，我们通过嵌套 <code>author1</code> 来创建一个 <code>post</code>。该程序输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Title:  Inheritance <span class="keyword">in</span> Go  </span><br><span class="line">Content:  Go supports composition instead of inheritance  </span><br><span class="line">Author:  Naveen Ramanathan  </span><br><span class="line">Bio:  Golang Enthusiast</span><br></pre></td></tr></table></figure>

<h2 id="结构体切片的嵌套"><a href="#结构体切片的嵌套" class="headerlink" title="结构体切片的嵌套"></a>结构体切片的嵌套</h2><p>我们可以进一步处理这个示例，使用博客帖子的切片来创建一个网站。</p>
<p>我们首先定义 <code>website</code> 结构体。请在上述代码里的 main 函数中，添加下面的代码，并运行它。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> website <span class="keyword">struct</span> &#123;  </span><br><span class="line">        []post</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(w website)</span> <span class="title">contents</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Contents of Website\n"</span>)</span><br><span class="line">    <span class="keyword">for</span> _, v := <span class="keyword">range</span> w.posts &#123;</span><br><span class="line">        v.details()</span><br><span class="line">        fmt.Println()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在你添加上述代码后，当你运行程序时，编译器将会报错，如下所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">main.go:31:9: syntax error: unexpected [, expecting field name or embedded <span class="built_in">type</span></span><br></pre></td></tr></table></figure>

<p>这项错误指出了嵌套的结构体切片 <code>[]post</code>。错误的原因是结构体不能嵌套一个匿名切片。我们需要一个字段名。所以我们来修复这个错误，让编译器顺利通过。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> website <span class="keyword">struct</span> &#123;  </span><br><span class="line">        posts []post</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到，我给帖子的切片 <code>[]post</code> 添加了字段名 <code>posts</code>。</p>
<p>现在我们来修改主函数，为我们的新网站创建一些帖子吧。</p>
<p>修改后的完整代码如下所示：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> author <span class="keyword">struct</span> &#123;  </span><br><span class="line">    firstName <span class="keyword">string</span></span><br><span class="line">    lastName  <span class="keyword">string</span></span><br><span class="line">    bio       <span class="keyword">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(a author)</span> <span class="title">fullName</span><span class="params">()</span> <span class="title">string</span></span> &#123;  </span><br><span class="line">    <span class="keyword">return</span> fmt.Sprintf(<span class="string">"%s %s"</span>, a.firstName, a.lastName)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> post <span class="keyword">struct</span> &#123;  </span><br><span class="line">    title   <span class="keyword">string</span></span><br><span class="line">    content <span class="keyword">string</span></span><br><span class="line">    author</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(p post)</span> <span class="title">details</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Title: "</span>, p.title)</span><br><span class="line">    fmt.Println(<span class="string">"Content: "</span>, p.content)</span><br><span class="line">    fmt.Println(<span class="string">"Author: "</span>, p.fullName())</span><br><span class="line">    fmt.Println(<span class="string">"Bio: "</span>, p.bio)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> website <span class="keyword">struct</span> &#123;  </span><br><span class="line"> posts []post</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(w website)</span> <span class="title">contents</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Contents of Website\n"</span>)</span><br><span class="line">    <span class="keyword">for</span> _, v := <span class="keyword">range</span> w.posts &#123;</span><br><span class="line">        v.details()</span><br><span class="line">        fmt.Println()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    author1 := author&#123;</span><br><span class="line">        <span class="string">"Naveen"</span>,</span><br><span class="line">        <span class="string">"Ramanathan"</span>,</span><br><span class="line">        <span class="string">"Golang Enthusiast"</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    post1 := post&#123;</span><br><span class="line">        <span class="string">"Inheritance in Go"</span>,</span><br><span class="line">        <span class="string">"Go supports composition instead of inheritance"</span>,</span><br><span class="line">        author1,</span><br><span class="line">    &#125;</span><br><span class="line">    post2 := post&#123;</span><br><span class="line">        <span class="string">"Struct instead of Classes in Go"</span>,</span><br><span class="line">        <span class="string">"Go does not support classes but methods can be added to structs"</span>,</span><br><span class="line">        author1,</span><br><span class="line">    &#125;</span><br><span class="line">    post3 := post&#123;</span><br><span class="line">        <span class="string">"Concurrency"</span>,</span><br><span class="line">        <span class="string">"Go is a concurrent language and not a parallel one"</span>,</span><br><span class="line">        author1,</span><br><span class="line">    &#125;</span><br><span class="line">    w := website&#123;</span><br><span class="line">        posts: []post&#123;post1, post2, post3&#125;,</span><br><span class="line">    &#125;</span><br><span class="line">    w.contents()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的主函数中，我们创建了一个作者 <code>author1</code>，以及三个帖子 <code>post1</code>、<code>post2</code> 和 <code>post3</code>。我们最后通过嵌套三个帖子，在第 62 行创建了网站 <code>w</code>，并在下一行显示内容。</p>
<p>程序会输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">Contents of Website</span><br><span class="line"></span><br><span class="line">Title:  Inheritance <span class="keyword">in</span> Go  </span><br><span class="line">Content:  Go supports composition instead of inheritance  </span><br><span class="line">Author:  Naveen Ramanathan  </span><br><span class="line">Bio:  Golang Enthusiast</span><br><span class="line"></span><br><span class="line">Title:  Struct instead of Classes <span class="keyword">in</span> Go  </span><br><span class="line">Content:  Go does not support classes but methods can be added to structs  </span><br><span class="line">Author:  Naveen Ramanathan  </span><br><span class="line">Bio:  Golang Enthusiast</span><br><span class="line"></span><br><span class="line">Title:  Concurrency  </span><br><span class="line">Content:  Go is a concurrent language and not a parallel one  </span><br><span class="line">Author:  Naveen Ramanathan  </span><br><span class="line">Bio:  Golang Enthusiast</span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/go/2-Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/11-%E6%95%B0%E7%BB%84%E5%92%8C%E5%88%87%E7%89%87/">
        <span class="nav-arrow">← </span>
        
          go/2-Go语言基础/11-数组和切片
        
      </a>
    
    
      <a class="nav-right" href="/go/3-Go%E8%AF%AD%E8%A8%80%E5%B8%B8%E7%94%A8%E6%A0%87%E5%87%86%E5%BA%93/02-Go%E8%AF%AD%E8%A8%80%E4%B9%8Bflag/">
        
          Go语言之flag
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#通过嵌套结构体进行组合"><span class="toc-nav-text">通过嵌套结构体进行组合</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#结构体切片的嵌套"><span class="toc-nav-text">结构体切片的嵌套</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/go/2-Go语言基础/27-组合取代继承/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "go/2-Go语言基础/27-组合取代继承",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>